package com.jn.exam.oss.service.impl;


import com.baidu.aip.util.Base64Util;
import com.jn.exam.oss.Face.BaiduAIFace;
import com.jn.exam.oss.SetingModel.Setingmodel;
import com.jn.exam.oss.service.FaceService;
import com.jn.exam.oss.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
public class FaceServiceImpl implements FaceService {
    @Autowired
    private BaiduAIFace faceapi;
    @Autowired
    private Setingmodel setingmodel;

    //人脸搜索1:N
    //指定用户id，和用户组搜索
    public Map<String, Object> searchfaceOne(String imagebase64, String userId, String faceGroupId) throws IOException {
        setingmodel.setImgpath(imagebase64);
        setingmodel.setUserID(userId);
        setingmodel.setGroupID(faceGroupId);
        Map map = faceapi.FaceSearchOne(setingmodel);
        return map;
    }

    //人脸搜索M:N
    public Map<String, Object> searchFaceSome(StringBuffer imagebase64, String faceGroupId) throws IOException {
        String substring = imagebase64.substring(imagebase64.indexOf(",") + 1, imagebase64.length());
        setingmodel.setImgpath(substring);
        setingmodel.setGroupID(faceGroupId);
        Map map = faceapi.FaceSearchSome(setingmodel);
        return map;
    }

    //人脸检测
    @Override
    public Map<String, Object> faceCheck(String imagebase64) throws IOException {
        setingmodel.setImgpath(imagebase64);
        Map map = faceapi.FaceCheck(setingmodel);
        return map;
    }

    //人脸注册
    @Override
    public Map<String, Object> registerFace(StringBuffer imagebase64, String userId, String faceGroupId) throws IOException {
        String substring = imagebase64.substring(imagebase64.indexOf(",") + 1, imagebase64.length());
        setingmodel.setImgpath(substring);
        setingmodel.setUserID(userId);
        setingmodel.setGroupID(faceGroupId);
        Map map = faceapi.FaceRegistration(setingmodel);
        return map;
    }

    //人脸更新
    @Override
    public Map<String, Object> updateFace(StringBuffer imagebast64, String userId, String faceGroupId) throws IOException {
        String substring = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        setingmodel.setImgpath(substring);
        setingmodel.setUserID(userId);
        setingmodel.setGroupID(faceGroupId);
        Map map = faceapi.FaceUpdate(setingmodel);
        return map;
    }

    //用于将用户从某个组中删除
    @Override
    public Map<String, Object> deleteUserFromGroup(String userId, String faceGroupId) throws Exception {
        setingmodel.setUserID(userId);
        setingmodel.setGroupID(faceGroupId);
        Map map=faceapi.deleteUserFromGroup(setingmodel);
        return map;
    }
}
